*> 100 Cool Things - http://cobolrocks.codeplex.com/
*> Copyright (c) 2009
*> by Emerson Lopes - emersonlopes@gmail.com
*>
*> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
*> documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
*> the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
*> to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*>
*> The above copyright notice and this permission notice shall be included in all copies or substantial portions 
*> of the Software.
*>
*> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
*> TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
*> THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
*> CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
*> DEALINGS IN THE SOFTWARE.

CLASS-ID. TwitterPost AS "CoolThings.TwitterPost".

    environment division.
        configuration section.
            repository.
                class     SystemWebHttpUtility      as "System.Web.HttpUtility"
				class     SystemString              as "System.String"
				class     SystemInteger             as "System.Int32"
				class     NetworkCredential         as "System.Net.NetworkCredential"
				class     NetWebRequest             as "System.Net.WebRequest"
				class     NetWebResponse            as "System.Net.WebResponse"
				class     WebHeaderCollection       as "System.Net.WebHeaderCollection"
				class     ClassByteArray            as "System.Byte[]"
				class     SystemTextEncoding        as "System.Text.Encoding"
				class     SystemIOStream            as "System.IO.Stream"
				class     SystemIOStreamReader      as "System.IO.StreamReader"
				class     ClassServicePointManager  as "System.Net.ServicePointManager"
				
				class     ClassWebException         as "System.Net.WebException"
				
				property  Credentials               as "Credentials"
				property  ContentType               as "ContentType"
				property  ContentLenght             as "ContentLength"
				property  ArrayLength               as "Length"
				property  RequestMethod             as "Method"
				property  Headers                   as "Headers"
				property  Expect100Continue         as "Expect100Continue".
                
 *> Instance's data and methods
    object.
       data division.
            working-storage section.
      
       procedure division.

             method-id. PostRequest as "PostRequest".   
             
                 data division.
                    
                     working-storage section.
                     
                     01 WebHttpUtility        usage object reference SystemWebHttpUtility.
                     01 servicePointManager   usage object reference ClassServicePointManager.
                     01 request               usage object reference NetWebRequest.
                     01 response              usage object reference NetWebResponse.
                     01 requestCredential     usage object reference NetworkCredential.
                     01 requestHeaders        usage object reference WebHeaderCollection.
                     01 byteArray             usage object reference ClassByteArray.
                     01 utf8Encoding          usage object reference SystemTextEncoding.
                     01 requestStream         usage object reference SystemIOStream.
                     01 responseStream        usage object reference SystemIOStream.
                     01 reader                usage object reference SystemIOStreamReader.
                     01 bytesLength           usage object reference SystemInteger.
                     
                     01 webException          usage object reference ClassWebException.
                     
                     01 twitterSTatus         usage object reference SystemString.
                     
			         linkage section.
				     01 url                usage object reference SystemString.
				     01 userName           usage object reference SystemString. 
				     01 userPassword       usage object reference SystemString.
				     01 userText           usage object reference SystemString.
				     01 result             usage object reference SystemString.
			 
			     procedure division using url, userName, UserPassword, userText returning result. 
			     
			         *> http://blogs.msdn.com/shitals/archive/2008/12/27/9254245.aspx
			         
			         set Expect100Continue of ClassServicePointManager   to   b"0"
			         
			         invoke NetWebRequest "Create" using url returning request 
			         
			         invoke NetworkCredential "NEW" using userName, userPassword returning requestCredential
			         
			         set    Credentials   of request   to  requestCredential
			         set    ContentType   of request   to  "application/x-www-form-urlencoded"
			         set    RequestMethod of request   to  "POST"
			         
			         set    requestHeaders             to  Headers of request
			         
			         invoke requestHeaders "Add" using "X-Twitter-Client",  "OO Cobol"  
			         invoke requestHeaders "Add" using "X-Twitter-Version", "1.0"  
			         invoke requestHeaders "Add" using "X-Twitter-URL",     "http://www.100coolthings.net"  
			         
			         invoke SystemTextEncoding "GetEncoding" using n"UTF-8" returning utf8Encoding

			         invoke SystemWebHttpUtility "UrlEncode" using userText returning userText
			         
			         set    twitterStatus  to  SystemString::"Concat"("status=", userText)
			         
			         invoke utf8Encoding "GetBytes" using twitterStatus returning byteArray  
			         
			         set    ContentLenght of request   to  ArrayLength of byteArray 

                     invoke request "GetRequestStream" returning requestStream
                     
                     set    bytesLength                to  ArrayLength of byteArray
                     
                     invoke requestStream "Write" using byteArray, 0, bytesLength 

                     try 
                         invoke request "GetResponse" returning response
                     
                     catch webException
                           continue
                     end-try
                  
                     invoke response "GetResponseStream" returning responseStream
                     
                     invoke SystemIOStreamReader "NEW" using responseStream returning reader
                     
                     invoke reader "ReadToEnd" returning result.

            end method PostRequest.               
    end object.
END CLASS TwitterPost.         